package cn.dzhy.myzf.qq.email.api;

import cn.dzhy.myzf.qq.email.model.User;
import cn.dzhy.myzf.qq.email.model.UserStatus;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.ApiResult;
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.token.TokenManager;
import java.util.ArrayList;
import java.util.List;

/**
 * 管理成员
 * @see__
 * @className UserApi
 * @author myzf(yangzhuangfei_min@odao.com)
 * @date 2018/3/15 下午10:48
 * @since JDK 1.8
 * @see cn.dzhy.myzf.qq.email.api
 * @return
 * @throws WeixinException
 */
public class UserApi extends EmailApi {
	private final TokenManager tokenManager;
	public UserApi(TokenManager tokenManager) {
		this.tokenManager = tokenManager;

	}
	/**
	 * 创建成员
	 * @see__https://exmail.qq.com/qy_mng_logic/doc#10014
	 * @className UserApi
	 * @author myzf(yangzhuangfei_min@odao.com)
	 * @date 2018/3/15 下午10:48
	 * @since JDK 1.8
	 * @see cn.dzhy.myzf.qq.email.api
	 * @return 
	 * @throws WeixinException
	 */
	public ApiResult createUser(User user) throws WeixinException {
		String user_create_uri = getRequestUri("user_create_uri");
		return excute(user_create_uri, user);
	}
	
	/**
	 * 更新成员
	 * @see__https://exmail.qq.com/qy_mng_logic/doc#10015
	 * @className UserApi
	 * @author myzf(yangzhuangfei_min@odao.com)
	 * @date 2018/3/15 下午10:50
	 * @since JDK 1.8
	 * @see cn.dzhy.myzf.qq.email.api
	 * @return 微信返回码
	 * @throws WeixinException
	 */
	public ApiResult updateUser(User user) throws WeixinException {
		String user_update_uri = getRequestUri("user_update_uri");
		return excute(user_update_uri, user);
	}

	/**
	 * 删除成员
	 * @see__https://exmail.qq.com/qy_mng_logic/doc#10016
	 * @className UserApi
	 * @author myzf(yangzhuangfei_min@odao.com)
	 * @date 2018/3/15 下午10:50
	 * @since JDK 1.8
	 * @see cn.dzhy.myzf.qq.email.api
	 * @return 
	 * @throws WeixinException
	 */
	public ApiResult deleteUser(String userid) throws WeixinException {
		String user_delete_uri = getRequestUri("user_delete_uri");
		Token token = tokenManager.getCache();
		WeixinResponse response = weixinExecutor.get(String.format(
				user_delete_uri, token.getAccessToken(), userid));
		return response.getAsResult();
	}


	/**
	 * 获取成员
	 * @see__https://exmail.qq.com/qy_mng_logic/doc#10017
	 * @className UserApi
	 * @author myzf(yangzhuangfei_min@odao.com)
	 * @date 2018/3/15 下午10:51
	 * @since JDK 1.8
	 * @see cn.dzhy.myzf.qq.email.api
	 * @return
	 * @throws WeixinException
	 */
	public User getUser(String userid) throws WeixinException {
		String user_get_uri = getRequestUri("user_get_uri");
		Token token = tokenManager.getCache();
		WeixinResponse response = weixinExecutor.get(String.format(
				user_get_uri, token.getAccessToken(), userid));
		JSONObject obj = response.getAsJson();
		User user = JSON.toJavaObject(obj, User.class);
		return user;
	}


	/**
	 * 获取部门成员 获取部门成员（详情）
	 * @see__https://exmail.qq.com/qy_mng_logic/doc#10018
	 * @className UserApi
	 * @author myzf(yangzhuangfei_min@odao.com)
	 * @date 2018/3/15 下午10:51
	 * @since JDK 1.8
	 * @see cn.dzhy.myzf.qq.email.api
	 * @return
	 * @throws WeixinException
	 */
	public List<User> listUser(Long partyId,boolean fetch_child,boolean findDetail) throws WeixinException {
		String user_list_uri = findDetail ? getRequestUri("user_list_uri") : getRequestUri("user_slist_uri");
		Token token = tokenManager.getCache();
		WeixinResponse response = weixinExecutor.get(String.format(user_list_uri, token.getAccessToken(), partyId, fetch_child ? 1 : 0));
		List<User> list = null;
		if(findDetail){
			JSONArray arrays = response.getAsJson().getJSONArray("userlist");
			list = new ArrayList<User>(arrays.size());
			for (int i = 0; i<arrays.size();i++){
				JSONObject obj = arrays.getJSONObject(i);
				User user = JSON.toJavaObject(obj, User.class);
				list.add(user);
			}
		}else {
			list = JSON.parseArray(response.getAsJson().getString("userlist"),User.class);
		}
		return list;
	}


	/**
	 * 批量检查帐号
	 * @see__https://exmail.qq.com/qy_mng_logic/doc#10020
	 * @className UserApi
	 * @author myzf(yangzhuangfei_min@odao.com)
	 * @date 2018/3/15 下午10:52
	 * @since JDK 1.8
	 * @see cn.dzhy.myzf.qq.email.api
	 * @return
	 * @throws WeixinException
	 */
	public List<UserStatus> batchcheckUser(List<String> userlist)
			throws WeixinException {
		JSONObject obj = new JSONObject();
		obj.put("userlist", userlist);
		String user_batchcheck_uri = getRequestUri("user_batchcheck_uri");
		Token token = tokenManager.getCache();
		WeixinResponse response = weixinExecutor.post(
				String.format(user_batchcheck_uri, token.getAccessToken()),
				obj.toJSONString());
		List<UserStatus> list = null;
		JSONArray arrays = response.getAsJson().getJSONArray("list");
		list = new ArrayList<UserStatus>(arrays.size());
		for (int i = 0; i<arrays.size();i++){
			JSONObject object = arrays.getJSONObject(i);
			UserStatus user = JSON.toJavaObject(object, UserStatus.class);

			list.add(user);
		}

		return list;
	}


	private ApiResult excute(String uri, User user)
			throws WeixinException {
		/**
		  * @Author: myzf
		  * @Date: 2018/3/15 下午10:49
		  * @param uri user 
		 * @param user
		*/
		JSONObject obj = (JSONObject) JSON.toJSON(user);
		Token token = tokenManager.getCache();
		WeixinResponse response = weixinExecutor.post(
				String.format(uri, token.getAccessToken()), obj.toJSONString());
		return response.getAsResult();
	}


}
